package com.bocloud.cmp.driver.providers.jd;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.providers.RouterDriver;
import com.bocloud.cmp.model.RouteModel;
import com.bocloud.cmp.model.RouterModel;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;
import com.jcloud.cap.client.ClientConfig;
import com.jcloud.cap.client.IClient;
import com.jcloud.cap.client.JCloudClient;
import com.jcloud.cap.client.builder.v1.CreateRouterRequestBuilder;
import com.jcloud.cap.client.builder.v1.DeleteRouterRequestBuilder;
import com.jcloud.cap.client.builder.v1.QueryRouterDetailRequestBuilder;
import com.jcloud.cap.client.builder.v1.QueryRoutersRequestBuilder;
import com.jcloud.cap.client.exception.ClientException;
import com.jcloud.cap.client.exception.ServerException;
import com.jcloud.cap.client.http.HttpConfig;
import com.jcloud.cap.data.model.v1.CreateRouterResponse;
import com.jcloud.cap.data.model.v1.DeleteRouterResponse;
import com.jcloud.cap.data.model.v1.QueryRouterDetailResponse;
import com.jcloud.cap.data.model.v1.QueryRouterDetailResponse.Router;
import com.jcloud.cap.data.model.v1.QueryRoutersResponse;

public class JdRouterDriver extends JdDriver implements RouterDriver {
	private static Logger logger = LoggerFactory.getLogger(JdRouterDriver.class);
	private IClient client;

	/**
	 * @param accessKey
	 * @param secretKey
	 * @param endpoint
	 */
	public JdRouterDriver(String accessKey, String secretKey, String endpoint, String region) {
		super(accessKey, secretKey, endpoint, region);
		ClientConfig config = new ClientConfig();
		HttpConfig httpConfig = new HttpConfig();
		// 调用地址
		httpConfig.setEndpoint(endpoint);
		httpConfig.setReadTimeout(20000);
		httpConfig.setConnectTimeout(20000);
		config.setHttpConfig(httpConfig);
		config.setCredential(this.getCredential().getCapCredential());
		client = new JCloudClient(config);
	}

	@Override
	public BsmResult sync() {
		QueryRoutersRequestBuilder builder = QueryRoutersRequestBuilder.create();
		this.init(builder, "network", "queryRouters", this.getRegion());
		try {
			QueryRoutersResponse response = client.send(builder.build());
			List<RouterModel> models = Lists.newArrayList();
			List<com.jcloud.cap.data.model.v1.RouterResponse.Router> routers = response.getRouters();
			if (null != routers) {
				for (com.jcloud.cap.data.model.v1.RouterResponse.Router router : routers) {
					RouterModel model = new RouterModel(router.getRouterId(), router.getName(), router.getDescription(),
							router.getCreateTime(), router.getFirewallId(), router.getFloatingIp(), null);
					models.add(model);
				}
			}
			return new BsmResult(true, models, "success");
		} catch (ClientException | ServerException e) {
			logger.error("List router [{}] exception!", e);
			return new BsmResult(false, "List router failed");
		}
	}

	@Override
	public BsmResult create(RouteModel name) {
		CreateRouterRequestBuilder builder = CreateRouterRequestBuilder.create();
		this.init(builder, "network", "createRouter", this.getRegion());
		try {
			CreateRouterResponse response = client.send(builder.name(name.getName()).build());
			return detail(response.getRouterId(), response.getRequestId());
		} catch (ClientException | ServerException e) {
			logger.error("Create router [{}] exception!", e);
			return new BsmResult(false, "Create router failed");
		}
	}

	@Override
	public BsmResult remove(String routerId) {
		DeleteRouterRequestBuilder builder = DeleteRouterRequestBuilder.create();
		this.init(builder, "network", "deleteRouter", this.getRegion());
		try {
			DeleteRouterResponse response = client.send(builder.routerId(routerId).build());
			return new BsmResult(true, response.getRequestId(), "success");
		} catch (ClientException | ServerException e) {
			logger.error("Remove router [{}] exception!", e);
			return new BsmResult(false, "Remove router failed");
		}
	}

	@Override
	public BsmResult detail(String routerId) {
		return detail(routerId, null);
	}

	private BsmResult detail(String routerId, String requestId) {
		QueryRouterDetailRequestBuilder builder = QueryRouterDetailRequestBuilder.create();
		this.init(builder, "network", "queryRouterDetail", this.getRegion());
		try {
			QueryRouterDetailResponse response = client.send(builder.routerId(routerId).build());
			Router router = response.getRouter();
			if (null == router) {
				return new BsmResult(false, "该对象不存在");
			}
			RouterModel model = new RouterModel(router.getRouterId(), router.getName(), router.getDescription(),
					router.getCreateTime(), router.getFirewallId(), router.getFloatingIp(), requestId);
			return new BsmResult(true, model, "success");
		} catch (ClientException | ServerException e) {
			logger.error("Query router [{}] exception!", e);
			return new BsmResult(false, "Query router failed");
		}
	}

	@Override
	public BsmResult modify(RouteModel routeModel) {
		// TODO Auto-generated method stub
		return null;
	}

}
